<?php

namespace App\Console\Commands\Wanggui;

use App\Enums\OpenShop\FeishuMini\ProgrammingEnum;
use App\Models\OpenShop\Common\SiteLocationMallModel;
use App\Models\Wanggui\KfShopSettleInSituationModel;
use App\Service\OpenShop\SiteLocation\GuanyuanDataService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class KfShopSettleInSituation extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wanggui:refresh-kf-shop-settle-in-situation';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '刷新夸父门店的入驻情况';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        ini_set('memory_limit', '512M');
        //截断整个表
        KfShopSettleInSituationModel::query()->truncate();
        //获得夸父门店列表
        try {
            GuanyuanDataService::setTimeOut(30);
            $shopList = array_column(GuanyuanDataService::newShopList(), null, '铺位号');
        } catch (\Exception $exception) {
            $shopList = [];
        }
        $number = 0;
        SiteLocationMallModel::query()
            ->where('grade', '>=', ProgrammingEnum::GRADE_C)
            ->chunk(200, function ($arr) use (&$number, $shopList) {
                $insertData = [];
                foreach ($arr as $value) {
                    $count   = 0;
                    $dotList = Redis::georadius('tiger:wanggui:dot', $value->lng, $value->lat, '200', 'm');
                    $shopNameArr = [];
                    foreach ($dotList as $dot) {
                        if (mb_strpos($dot, 'KFZC') === 0 && substr($dot, 4, 1) != 0) {
                            $shopCode = substr($dot, -4);
                            $shopNameArr[] = $shopList[$shopCode]['门店名称'] ?? $shopCode;
                            $count++;
                        }
                    }
                    $value->shopNum = $count;
                    if ($count > 0) {
                        $value->settle = ProgrammingEnum::SETTLE_STATUS_OK;
                    } else {
                        $value->settle = ProgrammingEnum::SETTLE_STATUS_OFF;
                    }
                    $value->save();
                    $insertData[] = [
                        'type'          => KfShopSettleInSituationModel::TYPE_MALL,
                        'name'          => $value->mallName,
                        'grade'         => ProgrammingEnum::GRADE_MAPPING[$value->grade],
                        'province'      => $value->province,
                        'city'          => $value->city,
                        'district'      => $value->district,
                        'address'       => $value->address,
                        'isSettleIn'    => $count ? 1 : 0,
                        'settleInCount' => $count,
                        'settleInShopName' => implode(',', $shopNameArr),
                        'createdAt'     => time(),
                        'updatedAt'     => time(),
                    ];
                }
                //入库
                KfShopSettleInSituationModel::query()->insert($insertData);
                dump($number += 200);
            });
    }
}
